{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4d084544",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b22969bf",
   "metadata": {},
   "source": [
    "# pandas中有3中数据结构: Series是一维的,  DataFrame是二维 表格型的,   MulitIndex是三维的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09d8d4c5",
   "metadata": {},
   "source": [
    "## Series\n",
    "### 创建Series(data数据,index下标,dtype数据类型)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "eaebb14b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0    1\n",
       " 1    2\n",
       " 2    3\n",
       " dtype: int64,\n",
       " 1    张三\n",
       " 2    李四\n",
       " dtype: object,\n",
       " 0    0\n",
       " 1    1\n",
       " 2    2\n",
       " 3    3\n",
       " 4    4\n",
       " 5    5\n",
       " 6    6\n",
       " 7    7\n",
       " 8    8\n",
       " 9    9\n",
       " dtype: int64,\n",
       " 1班    1\n",
       " 2班    2\n",
       " 3班    3\n",
       " dtype: int64)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = pd.Series([1,2,3])    #列表创建\n",
    "b = pd.Series({1:\"张三\",2:\"李四\"}) #字典创建\n",
    "c = pd.Series(np.arange(10)) #array创建\n",
    "\n",
    "\n",
    "# 指定索引创建 index\n",
    "a2 = pd.Series([1,2,3],index=[\"{}班\".format(i) for i in [1,2,3]])\n",
    "\n",
    "a,b,c,a2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd54bbc1",
   "metadata": {},
   "source": [
    "### Series的属性(index,values,根据下标读取数据)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bdd79140",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2 = pd.Series([1,2,3],index=[\"{}班\".format(i) for i in [1,2,3]])\n",
    "\n",
    "a2\n",
    "a2.index\n",
    "a2.values\n",
    "# 利用索引来读数\n",
    "a2[\"1班\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ba892ab",
   "metadata": {},
   "source": [
    "## DateFrame(二维 表结构数据)\n",
    "### DateFrame的创建(data数据,index下标,columns列名称)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "9952fc63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "      <th>e</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>同学1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>同学2</th>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>同学3</th>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>同学4</th>\n",
       "      <td>15</td>\n",
       "      <td>16</td>\n",
       "      <td>17</td>\n",
       "      <td>18</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>同学5</th>\n",
       "      <td>20</td>\n",
       "      <td>21</td>\n",
       "      <td>22</td>\n",
       "      <td>23</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>同学6</th>\n",
       "      <td>25</td>\n",
       "      <td>26</td>\n",
       "      <td>27</td>\n",
       "      <td>28</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>同学7</th>\n",
       "      <td>30</td>\n",
       "      <td>31</td>\n",
       "      <td>32</td>\n",
       "      <td>33</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>同学8</th>\n",
       "      <td>35</td>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>38</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>同学9</th>\n",
       "      <td>40</td>\n",
       "      <td>41</td>\n",
       "      <td>42</td>\n",
       "      <td>43</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>同学10</th>\n",
       "      <td>45</td>\n",
       "      <td>46</td>\n",
       "      <td>47</td>\n",
       "      <td>48</td>\n",
       "      <td>49</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       a   b   c   d   e\n",
       "同学1    0   1   2   3   4\n",
       "同学2    5   6   7   8   9\n",
       "同学3   10  11  12  13  14\n",
       "同学4   15  16  17  18  19\n",
       "同学5   20  21  22  23  24\n",
       "同学6   25  26  27  28  29\n",
       "同学7   30  31  32  33  34\n",
       "同学8   35  36  37  38  39\n",
       "同学9   40  41  42  43  44\n",
       "同学10  45  46  47  48  49"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "# import DataFrame as df\n",
    "\n",
    "col_list = [\"a\",\"b\",\"c\",\"d\",\"e\"]\n",
    "data = np.arange(50).reshape(10,5)\n",
    "df1 = pd.DataFrame(data\n",
    "                   ,index=[\"同学{}\".format(i+1) for i in range(data.shape[0])] \n",
    "                   ,columns=col_list)\n",
    "df1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aaed23f8",
   "metadata": {},
   "source": [
    "### DataFrame的属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "6e9d9c41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((10, 5),\n",
       " Index(['同学1', '同学2', '同学3', '同学4', '同学5', '同学6', '同学7', '同学8', '同学9', '同学10'], dtype='object'),\n",
       " Index(['a', 'b', 'c', 'd', 'e'], dtype='object'),\n",
       " array([[ 0,  1,  2,  3,  4],\n",
       "        [ 5,  6,  7,  8,  9],\n",
       "        [10, 11, 12, 13, 14],\n",
       "        [15, 16, 17, 18, 19],\n",
       "        [20, 21, 22, 23, 24],\n",
       "        [25, 26, 27, 28, 29],\n",
       "        [30, 31, 32, 33, 34],\n",
       "        [35, 36, 37, 38, 39],\n",
       "        [40, 41, 42, 43, 44],\n",
       "        [45, 46, 47, 48, 49]]))"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.shape,df1.index,  df1.columns, df1.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "1ddde739",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>同学1</th>\n",
       "      <th>同学2</th>\n",
       "      <th>同学3</th>\n",
       "      <th>同学4</th>\n",
       "      <th>同学5</th>\n",
       "      <th>同学6</th>\n",
       "      <th>同学7</th>\n",
       "      <th>同学8</th>\n",
       "      <th>同学9</th>\n",
       "      <th>同学10</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "      <td>20</td>\n",
       "      <td>25</td>\n",
       "      <td>30</td>\n",
       "      <td>35</td>\n",
       "      <td>40</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>11</td>\n",
       "      <td>16</td>\n",
       "      <td>21</td>\n",
       "      <td>26</td>\n",
       "      <td>31</td>\n",
       "      <td>36</td>\n",
       "      <td>41</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>12</td>\n",
       "      <td>17</td>\n",
       "      <td>22</td>\n",
       "      <td>27</td>\n",
       "      <td>32</td>\n",
       "      <td>37</td>\n",
       "      <td>42</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>13</td>\n",
       "      <td>18</td>\n",
       "      <td>23</td>\n",
       "      <td>28</td>\n",
       "      <td>33</td>\n",
       "      <td>38</td>\n",
       "      <td>43</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>4</td>\n",
       "      <td>9</td>\n",
       "      <td>14</td>\n",
       "      <td>19</td>\n",
       "      <td>24</td>\n",
       "      <td>29</td>\n",
       "      <td>34</td>\n",
       "      <td>39</td>\n",
       "      <td>44</td>\n",
       "      <td>49</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   同学1  同学2  同学3  同学4  同学5  同学6  同学7  同学8  同学9  同学10\n",
       "a    0    5   10   15   20   25   30   35   40    45\n",
       "b    1    6   11   16   21   26   31   36   41    46\n",
       "c    2    7   12   17   22   27   32   37   42    47\n",
       "d    3    8   13   18   23   28   33   38   43    48\n",
       "e    4    9   14   19   24   29   34   39   44    49"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#长宽表转置\n",
    "df1.T "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "305dfa46",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "      <th>e</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>同学7</th>\n",
       "      <td>30</td>\n",
       "      <td>31</td>\n",
       "      <td>32</td>\n",
       "      <td>33</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>同学8</th>\n",
       "      <td>35</td>\n",
       "      <td>36</td>\n",
       "      <td>37</td>\n",
       "      <td>38</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>同学9</th>\n",
       "      <td>40</td>\n",
       "      <td>41</td>\n",
       "      <td>42</td>\n",
       "      <td>43</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>同学10</th>\n",
       "      <td>45</td>\n",
       "      <td>46</td>\n",
       "      <td>47</td>\n",
       "      <td>48</td>\n",
       "      <td>49</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       a   b   c   d   e\n",
       "同学7   30  31  32  33  34\n",
       "同学8   35  36  37  38  39\n",
       "同学9   40  41  42  43  44\n",
       "同学10  45  46  47  48  49"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显示前多少行,最后多少行\n",
    "# df1.head(3), \n",
    "df1.tail(4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35551452",
   "metadata": {},
   "source": [
    "### DataFrame索引修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "5004e727",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(     b   c   d   e\n",
       " a                 \n",
       " 0    1   2   3   4\n",
       " 5    6   7   8   9\n",
       " 10  11  12  13  14\n",
       " 15  16  17  18  19\n",
       " 20  21  22  23  24\n",
       " 25  26  27  28  29\n",
       " 30  31  32  33  34\n",
       " 35  36  37  38  39\n",
       " 40  41  42  43  44\n",
       " 45  46  47  48  49,\n",
       "         c   d   e\n",
       " a  b             \n",
       " 0  1    2   3   4\n",
       " 5  6    7   8   9\n",
       " 10 11  12  13  14\n",
       " 15 16  17  18  19\n",
       " 20 21  22  23  24\n",
       " 25 26  27  28  29\n",
       " 30 31  32  33  34\n",
       " 35 36  37  38  39\n",
       " 40 41  42  43  44\n",
       " 45 46  47  48  49)"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col_list = [\"a\",\"b\",\"c\",\"d\",\"e\"]\n",
    "data = np.arange(50).reshape(10,5)\n",
    "df2 = pd.DataFrame(data\n",
    "                   ,columns=col_list)\n",
    "# df2\n",
    "\n",
    "#1.重新设定索引 (整体替换 ＋　reset_index)\n",
    "index_new = [\"同学{}\".format(i+1) for i in range(data.shape[0])] \n",
    "df2.index = index_new\n",
    "# 注:索引不可单个修改,是以整体存放,若修改 修改整体\n",
    "# df2\n",
    "\n",
    "df3=df2\n",
    "df3.reset_index()\n",
    "# df3\n",
    "\n",
    "# 2.指定某列作为新索引\n",
    "df2_2 = df2.set_index(\"a\") #需要开辟新空间\n",
    "df2_3 = df2.set_index([\"a\",\"b\"])\n",
    "df2_2,df2_3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81c4db98",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1fb5fdf7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "614662f3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "624d62b4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d44dad4c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c358936",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1620921",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
